package com.zzyl.nursing.controller.member;


import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.common.core.domain.AjaxResult;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IReservationService;
import io.swagger.annotations.ApiOperation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/member/reservation")
public class ReservationController extends BaseController {


    @Autowired
    private IReservationService reservationService;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;


    /**
     * 添加一个新的预订记录。
     *
     * @param reservation 预订信息对象，包含预订的详细数据
     * @return 返回操作成功的AjaxResult对象
     */
    //新增预约
    @PostMapping
    public AjaxResult add(@RequestBody Reservation reservation) {
        reservation.setStatus(0);
        reservationService.saveReservation(reservation);
        return AjaxResult.success();
    }

    /**
     * 查询预约信息列表。
     *
     * @param reservation 预约信息实体，包含查询条件。
     * @return 返回预约信息列表的封装结果。
     * @throws BaseException 如果用户没有预约信息或者查询结果为空，则抛出异常。
     */
    @GetMapping("/page")
    // 获取当前登录人
    public AjaxResult list(Reservation reservation) {
        Long userId = UserThreadLocal.get();
        System.out.println(userId);
        FamilyMember familyMember = familyMemberMapper.selectById(userId);
        String phone = familyMember.getPhone();
        if (phone == null) {
            throw new BaseException("暂无预约信息");
        }
        startPage();
        reservation.setMobile(phone);
        List<Reservation> reservations = reservationService.selectReservationList(reservation);
        if (reservations == null) {
            throw new BaseException("暂无预约信息");
        }
        TableDataInfo dataTable = getDataTable(reservations);
        return AjaxResult.success(dataTable);
    }


    @GetMapping("/cancelled-count")
    public AjaxResult cancelled() {
        Long userId = UserThreadLocal.get();
        Integer time = reservationService.cancelled(userId);
        return AjaxResult.success();
    }

    @GetMapping("/countByTime")
    public AjaxResult countByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        List<TimeVo> list = reservationService.countByTime(localDateTime);
        return AjaxResult.success(list);
    }

    /**
     * 取消指定ID的预约。
     *
     * @param id 预约的唯一标识符。
     * @return 返回操作成功的AjaxResult对象。
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id) {
        reservationService.cancelReservation(id);
        return AjaxResult.success();
    }
}
